package com.kayt.cyberBar.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;

/**
 * @author Kevin
 * @version 1.0
 * file path javacommons.util.NumberUtil.java
 * @since Aug 7, 2016 1:24:30 AM
 * description * @deprecated
 * 数值类型工具
 *
 */
public class NumberUtil {
    private static Logger log = LoggerFactory.getLogger(NumberUtil.class);

    /**
     * 获取两位数内的序列号
     *
     * @param sequence 整数
     * @return 返回数据
     */
    public static int getSequence(int sequence) {
        if (sequence > 99) {
            String seqStr = (sequence + "");
            seqStr = seqStr.substring(seqStr.length() - 2);
            sequence = getInt(seqStr);
        }
        return sequence;
    }

    public static String formatDouble(double number, String format) {
        String tmpret = "";
        try {
            DecimalFormat df = new DecimalFormat(format);
            df.format(number);
        } catch (Exception ex) {
            log.error(" formatDobule:" + ex);
        }
        return tmpret;
    }

    public static float getFloat(Object obj) {
        float tmpvalue = 0;
        try {
            tmpvalue = Float.parseFloat(obj.toString());
        } catch (Exception ex) {
            log.error("getDouble :" + ex);
        }
        return tmpvalue;
    }

    public static float getFloat(Object obj, float defvalue) {
        float tmpvalue = defvalue;
        try {
            tmpvalue = Float.parseFloat(obj.toString());
        } catch (Exception ex) {
            log.error("getDouble :" + ex);
        }
        return tmpvalue;
    }

    public static double getDouble(Object obj) {
        double tmpvalue = 0;
        try {
            tmpvalue = Double.parseDouble(obj.toString());
        } catch (Exception ex) {
            log.error("getDouble :" + ex);
        }
        return tmpvalue;
    }

    public static double getDouble(Object obj, double defvalue) {
        double tmpvalue = defvalue;
        try {
            tmpvalue = Double.parseDouble(obj.toString());
        } catch (Exception ex) {
            log.error("getDouble :" + ex);
        }
        return tmpvalue;
    }


    public static int getInt(Object obj, int defvalue) {
        int tmpret = defvalue;
        if (obj != null) {
            try {
                tmpret = Integer.parseInt(obj.toString());
            } catch (Exception ex) {
                log.error("",ex);
            }
        }
        return tmpret;
    }

    public static Integer getInteger(Object obj) {
        Integer tmpret = new Integer(0);
        if (obj != null) {
            try {
                tmpret = Integer.valueOf(obj.toString());
            } catch (Exception ex) {
                log.error("getInteger:" + ex);
            }
        }
        return tmpret;
    }

    public static Integer getInteger(Object obj, int defvalue) {
        Integer tmpret = new Integer(defvalue);
        if (obj != null) {
            try {
                tmpret = Integer.valueOf(obj.toString());
            } catch (Exception ex) {
                log.error("getInteger:" + ex);
            }
        }
        return tmpret;
    }

//    /**
//     * 判断对象是否可安全转为为整数
//     */
//  public static boolean isDia(Object obj) {
//    if (obj == null || ("" + obj).trim().equals("")) {
//      return false;
//    }
//    String s = "" + obj;
//    char[] c = s.toCharArray();
//    for (int i = 0; i < c.length; i++) {
//      if (c[i] > '9' || c[i] < '0') {
//        return false;
//      }
//    }
//    return true;
//  }
    public static int getInt(Object obj) {
        int tmp = 0;
        if (obj != null) {
            try {
                tmp = Integer.parseInt(obj.toString());
            } catch (Exception ex) {
                log.error("数据转换出错", ex);
            }
        }
        return tmp;
    }
}
